package com.summer.data.jql.compose;

import com.summer.data.Jsql;
import com.summer.data.dialect.Dialect;
import com.summer.data.jql.Context;
import com.summer.data.jql.behavior.FilterAble;
import com.summer.data.type.Linker;

/**
 * @author 345480567@qq.com
 */
public class FilterPack extends Linker<FilterAble, LOGIC_TYPE> implements FilterAble {

    public FilterPack and(FilterAble exp) {
        assert exp != null;
        if (size() != 0) {
            addR(LOGIC_TYPE.AND);
        }
        add(exp);
        return this;
    }

    public FilterPack or(FilterAble exp) {
        assert exp != null;
        if (size() != 0) {
            addR(LOGIC_TYPE.OR);
        }
        add(exp);
        return this;
    }

    @Override
    public Jsql toFilterJsql(Context ctx, Dialect dialect) {

        Jsql jsql = new Jsql();
        walk(new Walker<FilterAble, LOGIC_TYPE>() {
            @Override
            public void onEntity(FilterAble filterAble) {
                Jsql filterJsql = filterAble.toFilterJsql(ctx, dialect);
                jsql.append(filterJsql);
            }

            @Override
            public void onRelation(LOGIC_TYPE logicType) {
                Jsql.format(jsql, " %s ", logicType);
            }
        });

        return jsql;
    }
}
